#include "drawer.h"

Drawer::Drawer(QWidget *parent) : QWidget(parent)
{
}

QVector<QPointF> Drawer::obliqueParallelProjection()
{
    QVector<QPointF> retas;
    qreal theta = m_angle*M_PI/180;
    qreal cosT = cos(theta);
    qreal sinT = sin(theta);

    QList<Point3D*> retas3D = m_object3D.getLines();
    Point3D* p;
    foreach(p, retas3D) {

        qreal x =  ( p->x() - m_k*cosT*p->z() ) + m_originX;
        qreal y = -( p->y() - m_k*sinT*p->z() ) + m_originY;

        QPointF p2d(x, y);        
        retas.push_back(p2d);        

    }    

    return retas;
}

void Drawer::setProjection(qreal angulo, qreal k)
{
    m_angle = angulo;
    m_k = k;
}

void Drawer::setDimensions(int w, int h)
{
    m_width = w;
    m_height = h;
    m_originX = w/2;
    m_originY = h/2;
}

void Drawer::paintEvent(QPaintEvent *)
{
//    qDebug() << "===== paintEvent =====";

    QPainter painter(this);
    painter.setPen(Qt::red);
//    painter.drawRect(0, 0, 639, 438);

    QVector<QPointF> retas2D = obliqueParallelProjection();

    painter.drawPoint(m_originX, m_originY);

    painter.setPen(Qt::black);
    painter.drawLines(retas2D);

}

void Drawer::addObjectLine(qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2)
{
    m_object3D.addLine(x1,y1,z1,x2,y2,z2);
}

void Drawer::clearObject()
{
    m_object3D.clearObject();
}

void Drawer::translate(Point3D &point)
{
    m_object3D.translate(point);
}

void Drawer::scale(Point3D &pScale, Point3D &pReference)
{
    m_object3D.scale(pScale, pReference);
}

void Drawer::rotate(qreal &angle, int &axis, Point3D &pReference)
{
    m_object3D.rotate(angle, axis, pReference);
}

void Drawer::rotateAboutArbitraryAxis(qreal &angle, Point3D &p1Line, Point3D &p2Line)
{
    m_object3D.rotateAboutArbitraryAxis(angle, p1Line, p2Line);
}

void Drawer::reflect(int &axis)
{
    m_object3D.reflect(axis);
}

void Drawer::getCube()
{
    //front face
    m_object3D.addLine(-50, -50,  0  ,  50, -50,  0);
    m_object3D.addLine( 50, -50,  0  ,  50,  50,  0);
    m_object3D.addLine( 50,  50,  0  , -50,  50,  0);
    m_object3D.addLine(-50,  50,  0  , -50, -50,  0);

    //back face
    m_object3D.addLine(-50, -50, -100  ,  50, -50, -100);
    m_object3D.addLine( 50, -50, -100  ,  50,  50, -100);
    m_object3D.addLine( 50,  50, -100  , -50,  50, -100);
    m_object3D.addLine(-50,  50, -100  , -50, -50, -100);

    m_object3D.addLine(-50, -50,  0  , -50, -50, -100);
    m_object3D.addLine( 50, -50,  0  ,  50, -50, -100);
    m_object3D.addLine( 50,  50,  0  ,  50,  50, -100);
    m_object3D.addLine(-50,  50,  0  , -50,  50, -100);
}

void Drawer::getStar()
{
    //top
    m_object3D.addLine(-50,50,0,      0,150,-50);
    m_object3D.addLine(50,50,0,       0,150,-50);
    m_object3D.addLine(50,50,-100,    0,150,-50);
    m_object3D.addLine(-50,50,-100,   0,150,-50);

    //bottom
    m_object3D.addLine(-50,-50,0,      0,-150,-50);
    m_object3D.addLine(50,-50,0,       0,-150,-50);
    m_object3D.addLine(50,-50,-100,    0,-150,-50);
    m_object3D.addLine(-50,-50,-100,   0,-150,-50);

    //front
    m_object3D.addLine(-50,50,0,   0,0,100);
    m_object3D.addLine( 50,50,0,   0,0,100);
    m_object3D.addLine(50,-50,0,   0,0,100);
    m_object3D.addLine(-50,-50,0,   0,0,100);

    //back
    m_object3D.addLine(-50,50,-100,   0,0,-200);
    m_object3D.addLine( 50,50,-100,   0,0,-200);
    m_object3D.addLine(50,-50,-100,   0,0,-200);
    m_object3D.addLine(-50,-50,-100,   0,0,-200);

    //right
    m_object3D.addLine(50,50,0,      150,0,-50);
    m_object3D.addLine(50,50,-100,   150,0,-50);
    m_object3D.addLine(50,-50,-100,  150,0,-50);
    m_object3D.addLine(50,-50,0,     150,0,-50);

    //left
    m_object3D.addLine(-50,50,0,      -150,0,-50);
    m_object3D.addLine(-50,50,-100,   -150,0,-50);
    m_object3D.addLine(-50,-50,-100,  -150,0,-50);
    m_object3D.addLine(-50,-50,0,     -150,0,-50);
}
